package com.ruoyi.buss.qmcy.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.ruoyi.buss.report.domain.*;
import com.ruoyi.buss.report.service.*;
import com.ruoyi.buss.utils.ListUtil;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.buss.qmcy.domain.QmcyCustDkjg;
import com.ruoyi.buss.qmcy.service.IQmcyCustDkjgService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 贷款结构趋势Controller
 * 
 * @author sxy
 * @date 2021-07-16
 */
@RestController
@RequestMapping("/reportLsb")
public class QmcyCustDkjgController extends BaseController
{
    @Autowired
    private IQmcyCustDkjgService qmcyCustDkjgService;

    @Autowired
    private IReportDkjgDblxLsbService reportDkjgDblxLsbService;

    @Autowired
    private IReportDkjgDjkLsbService reportDkjgDjkLsbService;

    @Autowired
    private IReportDkjgZrrdhLsbService reportDkjgZrrdhLsbService;

    @Autowired
    private IReportBldkLsbService reportBldkLsbService;

    @Autowired
    private IReportDkywzlJmjtsxqylService reportDkywzlJmjtsxqylService;

    @Autowired
    private IReportXdywonlineLsbService reportXdywonlineLsbService;

    @Autowired
    private IReportXdzxsxLsbService reportXdzxsxLsbService;

    @Autowired
    private IReportDhyxLsbService reportDhyxLsbService;

    @Autowired
    private TokenService tokenService;


    /**
     * 查询贷款结构趋势列表
     */
    @PreAuthorize(hasPermi = "buss:dkjg:list")
    @GetMapping("/dkjg")
    public TableDataInfo list(QmcyCustDkjg qmcyCustDkjg)
    {   qmcyCustDkjg.setSjrq(ListUtil.getSjrq(qmcyCustDkjg.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();
        if(qmcyCustDkjg.getType() == null) {
            throw new CustomException("类型不能为空");
        }

        if (qmcyCustDkjg.getType() != null && qmcyCustDkjg.getType() != 0) {
            startPage("ETL_DT ASC");
        } else {
            startPage("ETL_DT DESC");
        }

        List<QmcyCustDkjg> list = qmcyCustDkjgService.selectQmcyCustDkjgList(qmcyCustDkjg,userInfo);
        return getDataTable(list);
    }

    /**
     * 贷款结构趋势-担保类型结构图4.3.1.1.1-3
     */
    @PreAuthorize(hasPermi = "buss:dblxjgt:list")
    @GetMapping("/dblxjgt")
    public TableDataInfo dblxjgt(ReportDkjgDblxLsb reportDkjgDblxLsb)
    {
        reportDkjgDblxLsb.setSjrq(ListUtil.getSjrq(reportDkjgDblxLsb.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();

        if (reportDkjgDblxLsb.getTypeNum() != null && reportDkjgDblxLsb.getTypeNum() != 0) {
            startPage("SJRQ ASC");
        } else {
            startPage("SJRQ DESC");
        }

        List<ReportDkjgDblxLsb> list = reportDkjgDblxLsbService.dblxjgt(reportDkjgDblxLsb,userInfo);
        return getDataTable(list);
    }
    /**
     * 导出担保类型结构图
     */
    @PreAuthorize(hasPermi = "buss:dkjg:dblxjgtexport")
    @Log(title = "导出担保类型结构图", businessType = BusinessType.EXPORT)
    @PostMapping("/dblxjgtexport")
    public void dblxjgtexport(HttpServletResponse response, ReportDkjgDblxLsb reportDkjgDblxLsb) throws IOException
    {
        reportDkjgDblxLsb.setSjrq(ListUtil.getSjrq(reportDkjgDblxLsb.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();

        List<ReportDkjgDblxLsb> list = reportDkjgDblxLsbService.dblxjgt(reportDkjgDblxLsb,userInfo);
        ExcelUtil<ReportDkjgDblxLsb> util = new ExcelUtil<ReportDkjgDblxLsb>(ReportDkjgDblxLsb.class);
        util.exportExcel(response, list, "担保类型结构图数据");
    }
    /**
     * 贷款结构趋势-担保类型结构图4.3.1.1.1-3
     */
    @PreAuthorize(hasPermi = "buss:djkjejg:list")
    @GetMapping("/djkjejg")
    public TableDataInfo djkjejg(ReportDkjgDjkLsb reportDkjgDjkLsb)
    {
        reportDkjgDjkLsb.setSjrq(ListUtil.getSjrq(reportDkjgDjkLsb.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();

        if (reportDkjgDjkLsb.getTypeNum() != null && reportDkjgDjkLsb.getTypeNum() !=0) {
            startPage("SJRQ ASC");
        } else {
            startPage("SJRQ DESC");
        }

        List<ReportDkjgDjkLsb> list = reportDkjgDjkLsbService.djkjejg(reportDkjgDjkLsb,userInfo);
        return getDataTable(list);
    }


    /**
     * 导出贷记卡
     */
    @PreAuthorize(hasPermi = "buss:dkjg:djkjejgExport")
    @Log(title = "导出贷记卡", businessType = BusinessType.EXPORT)
    @PostMapping("/djkjejgExport")
    public void djkjejgExport(HttpServletResponse response, ReportDkjgDjkLsb reportDkjgDjkLsb) throws IOException
    {
        reportDkjgDjkLsb.setSjrq(ListUtil.getSjrq(reportDkjgDjkLsb.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();

        List<ReportDkjgDjkLsb> list = reportDkjgDjkLsbService.djkjejg(reportDkjgDjkLsb,userInfo);
        ExcelUtil<ReportDkjgDjkLsb> util = new ExcelUtil<ReportDkjgDjkLsb>(ReportDkjgDjkLsb.class);
        util.exportExcel(response, list, "贷记卡数据");
    }

    /**
     * 贷款结构趋势-自然人单户或公类类单户4.3.1.1.1-5|6      参数 type必传
     */
    @PreAuthorize(hasPermi = "buss:zrrdh:list")
    @GetMapping("/zrrdh")
    public AjaxResult zrrdh(@Validated ReportDkjgZrrdhLsb reportDkjgZrrdhLsb)
    {
        reportDkjgZrrdhLsb.setSjrq(ListUtil.getSjrq(reportDkjgZrrdhLsb.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();
        Map<String,Object> map = new HashMap<>();

        if (reportDkjgZrrdhLsb.getTypeNum() != null && reportDkjgZrrdhLsb.getTypeNum() == 3) {
            startPage("SJRQ ASC");
        } else {
            startPage("SJRQ DESC");
        }

        List<ReportDkjgZrrdhLsb> list = reportDkjgZrrdhLsbService.zrrdh(reportDkjgZrrdhLsb,userInfo);

        if (reportDkjgZrrdhLsb.getTypeNum() != null && reportDkjgZrrdhLsb.getTypeNum() == 3) {

        } else {
            startPage("SJRQ ASC");
        }
        List<ReportDkjgZrrdhLsb> zrrdhhz = reportDkjgZrrdhLsbService.zrrdhhz(reportDkjgZrrdhLsb,userInfo);

        map.put("zrrdh",getDataTable(list));
        map.put("zrrdhhz",zrrdhhz);

        return AjaxResult.success(map);
    }

    /**
     * 导出自然人单户或公类类单户
     */
    @PreAuthorize(hasPermi = "buss:dkjg:zrrdhExport")
    @Log(title = "自然人单户或公类类单户", businessType = BusinessType.EXPORT)
    @PostMapping("/zrrdhExport")
    public void zrrdhExport(HttpServletResponse response, ReportDkjgZrrdhLsb reportDkjgZrrdhLsb) throws IOException
    {
        reportDkjgZrrdhLsb.setSjrq(ListUtil.getSjrq(reportDkjgZrrdhLsb.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();
        List<ReportDkjgZrrdhLsb> list = reportDkjgZrrdhLsbService.zrrdh(reportDkjgZrrdhLsb,userInfo);
        ExcelUtil<ReportDkjgZrrdhLsb> util = new ExcelUtil<ReportDkjgZrrdhLsb>(ReportDkjgZrrdhLsb.class);
        util.exportExcel(response, list, "自然人单户或公类类单户数据");
    }

    /**
     * 贷款结构趋势－不良贷款4.3.1.1.8
     */
    @PreAuthorize(hasPermi = "buss:bldk:list")
    @GetMapping("/bldk")
    public AjaxResult bldk(@Validated ReportBldkLsb reportBldkLsb)
    {
        reportBldkLsb.setSjrq(ListUtil.getSjrq(reportBldkLsb.getSjrq()));

        LoginUser userInfo = tokenService.getLoginUser();
        Map<String,Object> map = new HashMap<>();

        if (reportBldkLsb.getTypeNum() != null && reportBldkLsb.getTypeNum() == 3) {
            startPage("SJRQ ASC");
        } else {
            startPage("SJRQ DESC");
        }

        List<ReportBldkLsb> list = reportBldkLsbService.bldkList(reportBldkLsb,userInfo);

        if (reportBldkLsb.getTypeNum() != null && reportBldkLsb.getTypeNum() == 3) {
            startPage("SJRQ ASC");
        } else {
            startPage("SJRQ DESC");
        }
        List<ReportBldkLsb> bldkhz = reportBldkLsbService.bldkhz(reportBldkLsb,userInfo);

        map.put("bldkList",getDataTable(list));
        map.put("bldkhz",bldkhz);

        return AjaxResult.success(map);
    }


    /**
     * 导出不良贷款
     */
    @PreAuthorize(hasPermi = "buss:dkjg:bldkExport")
    @Log(title = "导出不良贷款", businessType = BusinessType.EXPORT)
    @PostMapping("/bldkExport")
    public void bldkExport(HttpServletResponse response, ReportBldkLsb reportBldkLsb) throws IOException
    {
        reportBldkLsb.setSjrq(ListUtil.getSjrq(reportBldkLsb.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();
        List<ReportBldkLsb> list = reportBldkLsbService.bldkList(reportBldkLsb,userInfo);
        ExcelUtil<ReportBldkLsb> util = new ExcelUtil<ReportBldkLsb>(ReportBldkLsb.class);
        util.exportExcel(response, list, "导出不良贷款");
    }

    /**
     * 4.3.1.1.1.2 -1|2 居民家庭授信签约率或用信签约率-信贷类报表1和2
     */
    @PreAuthorize(hasPermi = "buss:jtsxqyl:list")
    @GetMapping("/jtsxqyl")
    public AjaxResult jtsxqyl(@Validated ReportDkywzlJmjtsxqyl reportDkywzlJmjtsxqyl)
    {
        reportDkywzlJmjtsxqyl.setSjrq(ListUtil.getSjrq(reportDkywzlJmjtsxqyl.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();
        Map<String,Object> map = new HashMap<>();

        if (reportDkywzlJmjtsxqyl.getTypeNum() != null && reportDkywzlJmjtsxqyl.getTypeNum() == 3) {
            startPage("SJRQ ASC");
        } else {
            startPage("SJRQ DESC");
        }

        List<ReportDkywzlJmjtsxqyl> list = reportDkywzlJmjtsxqylService.jtsxqyl(reportDkywzlJmjtsxqyl,userInfo);

        if (reportDkywzlJmjtsxqyl.getTypeNum() != null && reportDkywzlJmjtsxqyl.getTypeNum() == 3) {
            startPage("SJRQ ASC");
        } else {
            startPage("SJRQ DESC");
        }
        List<ReportDkywzlJmjtsxqyl> jtsxqylhz = reportDkywzlJmjtsxqylService.jtsxqylhz(reportDkywzlJmjtsxqyl,userInfo);

        map.put("jtsxqyl",getDataTable(list));
        map.put("jtsxqylhz",jtsxqylhz);

        return AjaxResult.success(map);
    }

    /**
     * 导出居民家庭授信签约率或用信签约率
     */
    @PreAuthorize(hasPermi = "buss:dkjg:jtsxqylExport")
    @Log(title = "导出居民家庭授信签约率或用信签约率", businessType = BusinessType.EXPORT)
    @PostMapping("/jtsxqylExport")
    public void jtsxqylExport(HttpServletResponse response, ReportDkywzlJmjtsxqyl reportDkywzlJmjtsxqyl) throws IOException
    {
        reportDkywzlJmjtsxqyl.setSjrq(ListUtil.getSjrq(reportDkywzlJmjtsxqyl.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();
        List<ReportDkywzlJmjtsxqyl> list = reportDkywzlJmjtsxqylService.jtsxqyl(reportDkywzlJmjtsxqyl,userInfo);
        ExcelUtil<ReportDkywzlJmjtsxqyl> util = new ExcelUtil<ReportDkywzlJmjtsxqyl>(ReportDkywzlJmjtsxqyl.class);
        util.exportExcel(response, list, "导出居民家庭授信签约率或用信签约率");
    }

    /**
     * 4.3.1.1.1.3  线上信贷业务占比-信贷类报表3
     */
    @PreAuthorize(hasPermi = "buss:xsxdywzb:list")
    @GetMapping("/xsxdywzb")
    public AjaxResult xsxdywzb(@Validated ReportXdywonlineLsb reportXdywonlineLsb)
    {
        reportXdywonlineLsb.setSjrq(ListUtil.getSjrq(reportXdywonlineLsb.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();
        Map<String,Object> map = new HashMap<>();

        if (reportXdywonlineLsb.getTypeNum() != null && reportXdywonlineLsb.getTypeNum() == 3) {
            startPage("SJRQ ASC");
        } else {
            startPage("SJRQ DESC");
        }

        List<ReportXdywonlineLsb> list = reportXdywonlineLsbService.jtsxqyl(reportXdywonlineLsb,userInfo);

        if (reportXdywonlineLsb.getTypeNum() != null && reportXdywonlineLsb.getTypeNum() == 3) {
            startPage("SJRQ ASC");
        } else {
            startPage("SJRQ DESC");
        }
        List<ReportXdywonlineLsb> xsxdywzbhz = reportXdywonlineLsbService.jtsxqylhz(reportXdywonlineLsb,userInfo);

        map.put("xsxdywzb",getDataTable(list));
        map.put("xsxdywzbhz",xsxdywzbhz);

        return AjaxResult.success(map);
    }
    /**
     * 导出线上信贷业务占比
     */
    @PreAuthorize(hasPermi = "buss:dkjg:xsxdywzbExport")
    @Log(title = "线上信贷业务占比", businessType = BusinessType.EXPORT)
    @PostMapping("/xsxdywzbExport")
    public void xsxdywzbExport(HttpServletResponse response, ReportXdywonlineLsb reportXdywonlineLsb) throws IOException
    {
        reportXdywonlineLsb.setSjrq(ListUtil.getSjrq(reportXdywonlineLsb.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();
        List<ReportXdywonlineLsb> list = reportXdywonlineLsbService.jtsxqyl(reportXdywonlineLsb,userInfo);
        ExcelUtil<ReportXdywonlineLsb> util = new ExcelUtil<ReportXdywonlineLsb>(ReportXdywonlineLsb.class);
        util.exportExcel(response, list, "线上信贷业务占比");
    }
    /**
     * 整村授信签约率、用信率-4|5
     */
    @PreAuthorize(hasPermi = "buss:zcsxqyl:list")
    @GetMapping("/zcsxqyl")
    public AjaxResult zcsxqyl(ReportXdzxsxLsb reportXdzxsxLsb)
    {
        reportXdzxsxLsb.setSjrq(ListUtil.getSjrq(reportXdzxsxLsb.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();
        Map<String,Object> map = new HashMap<>();

        if (reportXdzxsxLsb.getTypeNum() != null && reportXdzxsxLsb.getTypeNum() == 3) {
            startPage("SJRQ ASC");
        } else {
            startPage("SJRQ DESC");
        }

        List<ReportXdzxsxLsb> list = reportXdzxsxLsbService.jtsxqyl(reportXdzxsxLsb,userInfo);

        if (reportXdzxsxLsb.getTypeNum() != null && reportXdzxsxLsb.getTypeNum() == 3) {
            startPage("SJRQ ASC");
        } else {
            startPage("SJRQ DESC");
        }
        List<ReportXdzxsxLsb> zcsxqylhz = reportXdzxsxLsbService.jtsxqylhz(reportXdzxsxLsb,userInfo);

        map.put("zcsxqyl",getDataTable(list));
        map.put("zcsxqylhz",zcsxqylhz);

        return AjaxResult.success(map);
    }

    /**
     * 导出整村授信签约率、用信率
     */
    @PreAuthorize(hasPermi = "buss:dkjg:zcsxqylExport")
    @Log(title = "整村授信签约率、用信率", businessType = BusinessType.EXPORT)
    @PostMapping("/zcsxqylExport")
    public void zcsxqylExport(HttpServletResponse response, ReportXdzxsxLsb reportXdzxsxLsb) throws IOException
    {
        reportXdzxsxLsb.setSjrq(ListUtil.getSjrq(reportXdzxsxLsb.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();
        List<ReportXdzxsxLsb> list = reportXdzxsxLsbService.jtsxqyl(reportXdzxsxLsb,userInfo);
        ExcelUtil<ReportXdzxsxLsb> util = new ExcelUtil<ReportXdzxsxLsb>(ReportXdzxsxLsb.class);
        util.exportExcel(response, list, "整村授信签约率、用信率");
    }

    /**
     * 单户用信100w以下的个人贷款- 6
     */
    @PreAuthorize(hasPermi = "buss:grdhyx:list")
    @GetMapping("/grdhyx")
    public AjaxResult grdhyx(ReportDhyxLsb reportDhyxLsb)
    {
        reportDhyxLsb.setSjrq(ListUtil.getSjrq(reportDhyxLsb.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();
        Map<String,Object> map = new HashMap<>();

        if (reportDhyxLsb.getTypeNum() != null && reportDhyxLsb.getTypeNum() == 3) {
            startPage("SJRQ ASC");
        } else {
            startPage("SJRQ DESC");
        }

        List<ReportDhyxLsb> list = reportDhyxLsbService.grdhyx(reportDhyxLsb,userInfo);

        if (reportDhyxLsb.getTypeNum() != null && reportDhyxLsb.getTypeNum() == 3) {

        } else {
            startPage("SJRQ ASC");
        }
        List<ReportDhyxLsb> grdhyxhz = reportDhyxLsbService.grdhyxhz(reportDhyxLsb,userInfo);

        map.put("grdhyx",getDataTable(list));
        map.put("grdhyxhz",grdhyxhz);

        return AjaxResult.success(map);
    }
    /**
     * 导出单户用信
     */
    @PreAuthorize(hasPermi = "buss:dkjg:grdhyxExport")
    @Log(title = "单户用信", businessType = BusinessType.EXPORT)
    @PostMapping("/grdhyxExport")
    public void grdhyxExport(HttpServletResponse response, ReportDhyxLsb reportDhyxLsb) throws IOException
    {
        reportDhyxLsb.setSjrq(ListUtil.getSjrq(reportDhyxLsb.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();
        List<ReportDhyxLsb> list = reportDhyxLsbService.grdhyx(reportDhyxLsb,userInfo);
        ExcelUtil<ReportDhyxLsb> util = new ExcelUtil<ReportDhyxLsb>(ReportDhyxLsb.class);
        util.exportExcel(response, list, "单户用信");
    }

    /**
     * 导出贷款结构趋势列表
     */
    @PreAuthorize(hasPermi = "buss:dkjg:dkjgexport")
    @Log(title = "贷款结构趋势", businessType = BusinessType.EXPORT)
    @PostMapping("/dkjgexport")
    public void export(HttpServletResponse response, QmcyCustDkjg qmcyCustDkjg) throws IOException
    {
        qmcyCustDkjg.setSjrq(ListUtil.getSjrq(qmcyCustDkjg.getSjrq()));
        LoginUser userInfo = tokenService.getLoginUser();

        List<QmcyCustDkjg> list = qmcyCustDkjgService.selectQmcyCustDkjgList(qmcyCustDkjg,userInfo);
        ExcelUtil<QmcyCustDkjg> util = new ExcelUtil<QmcyCustDkjg>(QmcyCustDkjg.class);
        util.exportExcel(response, list, "贷款结构趋势数据");
    }


}
